package com.cuitrip.business.notice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.cuitrip.apiservice.MessageBusiness;
import com.cuitrip.business.notice.ui.MessageMode;
import com.cuitrip.service.R;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.LogHelper;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by baziii on 15/8/19.
 */
public class MessageDetailActivity extends CustomUiFragmentActivity implements IProxyCallback{
    public static final String KEY = "KEY";
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.content)
    TextView content;
    private MessageMode mode;
    private ApiProxy mApiProxy = new ApiProxy(this);

    public static void start(Context context, MessageMode messageMode) {
        context.startActivity(new Intent(context, MessageDetailActivity.class).putExtra(KEY, messageMode));
    }

   /* @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_detail_v);
        mode = (MessageMode) getIntent().getSerializableExtra(KEY);
        showActionBar(mode.getType() == MessageMode.SYSTEM_TYPE ? getString(R.string.message_type_system):getString(R.string.message_type_book));

    }*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mode = (MessageMode) getIntent().getSerializableExtra(KEY);
        super.onCreate(savedInstanceState,R.layout.message_detail_v);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
//        time.setText(TimeUtil.formatMinutes(time.getContext(),mode.getDate(),"yyyy-MM-dd HH:mm"));
        time.setText(
                TimeUtil.formatTime(
                        InputTime.build(mode.getGmtCreated()),
                        OutputTime.getLocalInstance(OutputTime.OutputType.Type_Minute)
                )
        );
        content.setText(mode.getContent());
        setAsRead(mode.getId());
    }

    private void setAsRead(String messageid){
        MessageBusiness.setMessageAsRead(mApiProxy,messageid,"1");
    }


    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        if(response!=null){
            LogHelper.d("message_is_read",response.result.toString());
        }
        return true;
    }


    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=(mode.getType() == MessageMode.SYSTEM_TYPE ? getString(R.string.message_type_system):getString(R.string.message_type_book));
        return config;
    }

    @Override
    public void onTopbarLeftPress() {

        finish();
    }
}
